var g_iInfo = 0;
var g_aLanguage = [];

function WL_Settings(type){
	var hWSH = new ActiveXObject("WScript.Shell");
	var szRegistryPath = MsgPlus.ScriptRegPath + Messenger.MyUserId;
	switch(type){
		case WL_LOAD:
			var hPlC = g_hWMP.playlistCollection, szPl = "WLMini_" + Messenger.MyUserId;
			var hPl = hPlC.getByName(szPl);
			g_hWMP.currentPlaylist = hPl.count?hPl.item(0):hPlC.newPlaylist(szPl);
			try{
				for(i in g_oSettings)
					g_oSettings[i] = hWSH.RegRead(szRegistryPath + "\\" + i);
				
				g_hWMP.settings.volume = g_oSettings.VOLUME;
				g_hWMP.settings.setMode("loop", g_oSettings.LOOP);
				g_hWMP.settings.setMode("shuffle", g_oSettings.SHUFFLE);
				break;
				}catch(e){}
		case WL_SAVE:
			for(i in g_oSettings)
				hWSH.RegWrite(szRegistryPath + "\\" + i, g_oSettings[i]);
		break;
		}
	if(type == WL_LOAD)
		WL_LoadLanguage(g_oSettings.LANGUAGE);
	}

function WL_GetFiles(directory){
	var aFiles = [];
	for(e=new Enumerator(new ActiveXObject("Scripting.FileSystemObject").GetFolder(directory).Files);!e.atEnd();e.moveNext())
		aFiles.push(e.item());
	return aFiles;
	}

function WL_CreateWnd(windowid){return MsgPlus.CreateWnd("skins\\" + g_oSettings.PLAYER_SKIN, windowid)||MsgPlus.CreateWnd("skins\\WLMini.xml", windowid);}

function WL_SetAlpha(hWnd, lAmt){
	lAmt = parseInt(lAmt);
	Interop.Call("user32", "SetWindowLongW", hWnd, -20, 327956|(lAmt<255?0x80000:0));
	Interop.Call("user32", "SetLayeredWindowAttributes", hWnd, 0, lAmt, 0x2);
	}

function WL_SetNowPlaying(enabled, format, title, artist, album){
	var song = Interop.Allocate(512), copyDataStruct = Interop.Allocate(12), hMSGR=0;
	song.WriteString(0, "\\0Music\\0" + enabled + "\\0" + format + "\\0" + title + "\\0" + artist + "\\0" + album);
	copyDataStruct.WriteDWORD(0, 0x547);
	copyDataStruct.WriteDWORD(4, 512);
	copyDataStruct.WriteDWORD(8, song.DataPtr);
	do{Interop.Call("user32", "SendMessageW", hMSGR=Interop.Call("user32", "FindWindowExW", 0, hMSGR, "MsnMsgrUIManager", 0), 0x4A, 0, copyDataStruct);}
	while(hMSGR!=0);
	}

function WL_InfoUpdate(){
	var oMedia = g_hWMP.currentMedia;
	if(oMedia)
		WL_PLAYER.SetControlText("lblInfo", oMedia.getItemInfo(["title","WM/AlbumTitle","artist"][g_iInfo+=g_iInfo>1?-2:1]));
	MsgPlus.AddTimer("WL_InfoUpdate", 3000);
	}

function WL_DataUpdate(){
	var oMedia = g_hWMP.currentMedia;
	if(oMedia){
		WL_PLAYER.SetControlText("lblDuration", g_hWMP.controls.currentPositionString + (oMedia.duration?"/"+oMedia.durationString:""));
		WL_PLAYER.SendControlMessage("position", 0x406, 1, (oMedia.duration&0xFFFF)<<16);
		WL_PLAYER.SendControlMessage("position", 0x405, 1, g_hWMP.controls.currentPosition);
		}
	MsgPlus.AddTimer("WL_DataUpdate", 500);
	}

function WL_NP(media){
	var oItem = media||g_hWMP.currentMedia;
	return oItem.getItemInfo("title") + " - " + oItem.getItemInfo("artist");
	}

function WL_LoadPlaylist(){
	WL_ClearPlaylist();
	var oCurPlaylist = g_hWMP.currentPlaylist, n = oCurPlaylist.count;
	var oItem, NP, search = new RegExp(arguments[0], "i");
	for(i=0;i<n;i++){
		NP = WL_NP(oItem = oCurPlaylist.item(i));
		if(arguments[0] && !search.test(NP))
			continue;
		WL_PLAYLIST.LstView_AddItem("playlist", NP + " (" + (oItem.duration?oItem.durationString:"Stream") + ")", i);
		}
	}

function WL_ClearPlaylist(){Interop.Call("user32", "SendMessageW", WL_PLAYLIST.GetControlHandle("playlist"), 0x1009, 0, 0);}

function WL_DeletePlaylist(){
	if(arguments.length == 1)
		g_hWMP.currentPlaylist.removeItem(g_hWMP.currentPlaylist.item(WL_PLAYLIST.LstView_GetItemData("playlist", arguments[0])));
		else if(Interop.Call("user32", "MessageBoxW", WL_PLAYLIST.handle, WL_GetLang("PLAYLIST_CLEARWARNING"), "WLMini", 68) == 6)
		g_hWMP.currentPlaylist.clear();
	}

function WL_AddToPlaylist(){
	var OFD = OpenFileDialog("Add Music", WL_PLAYLIST.handle, "Music Files|*.mp3;*.wma;*.wpl;*.m3u;*.wav|All Files|*.*|");
	for(i in OFD)
		g_hWMP.currentPlaylist.appendItem(g_hWMP.newMedia(OFD[i]));
	}

function WL_SetButton(){
	if(WL_PLAYER){
		var bIsPlaying = g_hWMP.playState==WL_PLAY;
		Interop.Call("user32", "ShowWindow", WL_PLAYER.GetControlHandle("BtnPause"), bIsPlaying);
		Interop.Call("user32", "ShowWindow", WL_PLAYER.GetControlHandle("BtnPlay"), !bIsPlaying);
		}
	}

function WL_LoadLanguage(lang){
	var hLang = new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(MsgPlus.ScriptFilesPath + "\\langs\\" + lang), data;
	while(!hLang.AtEndOfStream){
		data = hLang.ReadLine().split(" = ");
		g_aLanguage[data[0]] = data[1];
		}
	hLang.Close();
	}

function WL_GetLang(id){return g_aLanguage[id]||id;}